package com.singing.manager;

import com.singing.model.Command;
import com.singing.util.AdminContext;

import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

// 抽象类，管理所有子类
public abstract class BaseManager {

    // 退出程序
    public static final int EXIT = -1;

    // 返回上一级
    public static final int BACK = 0;


    public static final int LOGIN_SUCCESS = Integer.MAX_VALUE;


    // 标题
    private String title;

    // 命令列表
    private List<Command> commandList;

    // 不需要登录的命令列表
    private List<Command> noNeedLoginCommandList;

    public BaseManager(String title, List<Command> commandList) {
        for (Command command : commandList) {
            if (command.getId() == BACK || command.getId() == EXIT) {
                throw new RuntimeException("Command id can not be " + BACK + " or " + EXIT);
            }
        }
        commandList.add(0, new Command(BACK, "返回", false));
        commandList.add(new Command(EXIT, "退出程序", false));

        this.title = title;
        this.commandList = commandList;

        noNeedLoginCommandList = commandList.stream().filter(command -> !command.isNeedLogin()).collect(Collectors.toList());
    }

    // 抽象方法，每个子类都要实现
    public final void manage() {

        Set<Integer> ids = noNeedLoginCommandList.stream().map(Command::getId).collect(Collectors.toSet());
        ;


        Scanner scanner = new Scanner(System.in);

        while (true) {

            printTitle();
            printCommandList();
            System.out.println();
            printTitleExtra();

            Integer command = null;

            if (AdminContext.isLogin()) {
                ids = commandList.stream().map(Command::getId).collect(Collectors.toSet());
            }

            String commandStr = scanner.next();
            try {
                command = Integer.valueOf(commandStr);
                if (!ids.contains(command)) {
                    throw new RuntimeException();
                }
            } catch (RuntimeException e) {
                System.err.println("输入错误，请重新输入");
                continue;
            }


            if (command == EXIT) {
                exit(0);
            }
            if (command == BACK) {
                break;
            }
            int command1 = __processCommand(command);

            if (command1 == LOGIN_SUCCESS) {
                break;
            }

//            if (exit == BACK) {
//                break;
//            }
        }
    }

    // 打印 标题
    protected void printTitle() {
        System.out.println();
        System.out.println(String.format("***********%s***********", title));
        System.out.println();
    }

    protected void printTitleExtra() {
    }

    // 打印命令列表
    protected void printCommandList() {
        List<Command> showCommandList = noNeedLoginCommandList;
        if (AdminContext.isLogin()) {
            showCommandList = commandList;
        }
        showCommandList.forEach(command -> {
            System.out.println(String.format("%d、%s", command.getId(), command.getName()));
        });
    }

    // 返回上一级
    protected int back() {
        return 0;
    }

    private int __processCommand(int command) {

        if (command == BACK) {
            return back();
        }
        if (command == EXIT) {
            exit(0);
        }

        return processCommand(command);
    }

    // 具体处理命令 抽象方法，每个子类都要实现
    protected abstract int processCommand(int command);


    // 退出程序
    private void exit(int status) {
        System.exit(status);
    }
}
